<?php
/*
 * Created on 2016/11/26/
 * 头像上传
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 session_start();
 require_once('../db/properties.inc.php');
 $userInfo = $_SESSION["userInfo"]; 
  $userObj = json_decode($userInfo);//用户信息
 if(!$userInfo){
 	header("Location: ../../view/login.html");
 	exit;
 }
if ($_FILES["imageName"]["size"] < 500000){//图片小于200kb
  if($_FILES["imageName"]["size"]==0){
  	 echo "2";
     exit;
  }
  if ($_FILES["imageName"]["error"] > 0){
    echo "0";
    exit;
   }else{
    if (file_exists("../../upload/" . $userObj->id .".jpg")){//判断是否存在图片
      	unlink("../../upload/" .  $userObj->id.".jpg");
     }
     //保存图片
     move_uploaded_file($_FILES["imageName"]["tmp_name"],"../../upload/" .  $userObj->id.".jpg");
     
     //保存用户头像
      $conn = new mysqli($url, $user, $pwd, $dbname, $port);
	  $conn -> set_charset("utf8");
	  if (mysqli_connect_errno()) {
	 	echo "0";
	    exit;
	  }
	 $address = "/upload/" .  $userObj->id.".jpg";
	 $sql = "UPDATE userInfo SET head=? WHERE id=?";
	 $mysqli_stmt=$conn->prepare($sql);
	 //第一个参数表明变量类型，有i(int),d(double),s(string),b(blob)
	 $mysqli_stmt->bind_param('si',$address,$userObj->id);
	 //执行预处理语句
	 $mysqli_stmt->execute();
	 $conn->close();
	 echo "2";
	 exit;
    }
  }else{
 	 echo "1";
 	 exit;
  }
?>
